import execjs
import requests
from lxml import etree
import re

with open('./aes.min.js', 'r') as f:
    jscontent = f.read()
context= execjs.compile(jscontent)


headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36',
    'cookie': ''
}

url = 'http://www.iwencai.com/search?w=火币&tid=news&st=1&p={}'
for i in range(1,11): # 请求10页
    headers['cookie'] = 'v={}'.format(context.call("v"))
    req = requests.get(url.format(i), headers=headers)
    html = etree.HTML(req.text)
    text = html.xpath("//div[@id='hidelist']//div[@class='s_r_box']")
    print('page{},{},cookie{}'.format(i,"*"*10, headers['cookie']))
    for t in text:
        titles = t.xpath(".//h2[@class='s_r_blue_title']//text()")
        title = ''
        for tit in titles:
            tit = re.sub(r'\n+|\t+|\r+|\s+', '', tit)
            title = title + tit
        print(title)
